package com.zhengbing.tj.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.ApiResponse;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhengbing.tj.common.ApiResult;
import com.zhengbing.tj.entity.Role;
import com.zhengbing.tj.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理控制器
 * 处理角色相关的REST请求
 *
 * @author zhengbing
 * @date 2024-08-22
 */
@Api(tags = "角色管理", description = "提供角色的增删改查及用户角色关联等功能")
@RestController
@RequestMapping("/api/roles")
public class RoleController {
    
    @Autowired
    private RoleService roleService;
    
    /**
     * 获取所有角色
     * 查询系统中的所有角色信息
     *
     * @return 角色列表
     */
    @ApiOperation(value = "获取所有角色", notes = "查询系统中的所有角色信息")
    @ApiResponses({
        @ApiResponse(code = 200, message = "查询成功", response = Role.class, responseContainer = "List")
    })
    @GetMapping
    public ApiResult<List<Role>> getAllRoles() {
        return ApiResult.success(roleService.list());
    }
    
    /**
     * 根据ID获取角色
     * 根据角色ID查询角色详情
     *
     * @param id 角色ID
     * @return 角色信息
     */
    @ApiOperation(value = "根据ID获取角色", notes = "根据角色ID查询角色详情")
    @ApiResponses({
        @ApiResponse(code = 200, message = "查询成功", response = Role.class),
        @ApiResponse(code = 404, message = "角色不存在")
    })
    @GetMapping("/{id}")
    public ApiResult<Role> getRoleById(@ApiParam(value = "角色ID", required = true, example = "1") @PathVariable Long id) {
        Role role = roleService.getById(id);
        if (role != null) {
            return ApiResult.success(role);
        } else {
            return ApiResult.error(404, "角色不存在");
        }
    }
    
    /**
     * 添加角色
     * 创建新角色
     *
     * @param role 角色信息
     * @return 添加结果
     */
    @ApiOperation(value = "添加角色", notes = "创建新角色")
    @ApiResponses({
        @ApiResponse(code = 200, message = "添加成功")
    })
    @PostMapping
    public ApiResult<String> addRole(@ApiParam(value = "角色信息", required = true) @RequestBody Role role) {
        roleService.save(role);
        return ApiResult.success("添加成功", null);
    }
    
    /**
     * 更新角色
     * 根据ID更新角色信息
     *
     * @param id 角色ID
     * @param role 角色信息
     * @return 更新结果
     */
    @ApiOperation(value = "更新角色", notes = "根据ID更新角色信息")
    @ApiResponses({
        @ApiResponse(code = 200, message = "更新成功"),
        @ApiResponse(code = 404, message = "角色不存在")
    })
    @PutMapping("/{id}")
    public ApiResult<String> updateRole(@ApiParam(value = "角色ID", required = true, example = "1") @PathVariable Long id, @ApiParam(value = "角色信息", required = true) @RequestBody Role role) {
        Role existing = roleService.getById(id);
        if (existing == null) {
            return ApiResult.error(404, "角色不存在");
        }
        role.setId(id);
        roleService.updateById(role);
        return ApiResult.success("更新成功", null);
    }
    
    /**
     * 删除角色
     * 根据ID删除角色
     *
     * @param id 角色ID
     * @return 删除结果
     */
    @ApiOperation(value = "删除角色", notes = "根据ID删除角色")
    @ApiResponses({
        @ApiResponse(code = 200, message = "删除成功")
    })
    @DeleteMapping("/{id}")
    public ApiResult<String> deleteRole(@ApiParam(value = "角色ID", required = true, example = "1") @PathVariable Long id) {
        roleService.removeById(id);
        return ApiResult.success("删除成功", null);
    }
    
    /**
     * 批量删除角色
     * 根据ID列表删除多个角色
     *
     * @param ids 角色ID列表
     * @return 删除结果
     */
    @ApiOperation(value = "批量删除角色", notes = "根据ID列表删除多个角色")
    @ApiResponses({
        @ApiResponse(code = 200, message = "删除成功")
    })
    @DeleteMapping("/batch")
    public ApiResult<String> deleteRoles(@ApiParam(value = "角色ID列表", required = true) @RequestBody List<Long> ids) {
        roleService.removeByIds(ids);
        return ApiResult.success("删除成功", null);
    }
    
    /**
     * 根据用户ID查询角色列表
     *
     * @param userId 用户ID
     * @return 角色列表
     */
    @ApiOperation(value = "根据用户ID查询角色列表", notes = "查询指定用户拥有的角色")
    @ApiResponses({
        @ApiResponse(code = 200, message = "查询成功", response = Role.class, responseContainer = "List")
    })
    @GetMapping("/user/{userId}")
    public ApiResult<List<Role>> getRolesByUserId(@ApiParam(value = "用户ID", required = true, example = "1") @PathVariable Long userId) {
        return ApiResult.success(roleService.getRolesByUserId(userId));
    }
    
    /**
     * 分配用户角色
     * 为指定用户分配角色
     *
     * @param userId 用户ID
     * @param roleIds 角色ID列表
     * @return 分配结果
     */
    @ApiOperation(value = "分配用户角色", notes = "为指定用户分配角色")
    @ApiResponses({
        @ApiResponse(code = 200, message = "分配成功")
    })
    @PostMapping("/user/{userId}/assign")
    public ApiResult<String> assignUserRoles(@ApiParam(value = "用户ID", required = true, example = "1") @PathVariable Long userId, @ApiParam(value = "角色ID列表", required = true) @RequestBody List<Long> roleIds) {
        roleService.saveUserRoles(userId, roleIds);
        return ApiResult.success("分配成功", null);
    }
}